﻿using IQIGame.Onigao.Framework;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class NumerCappedRange : IReusableClass
    {
        public float min { get; private set; }
        public float max { get; private set; }
        public float current { get; private set; }

        private float diffRange = 0;

        public bool isHasRange => diffRange > 0;
        public float ratio => isHasRange ? current / diffRange : 0;
        public int maxIntValue => (int)max;
        public int currentIntValue => (int)current;

        public NumerCappedRange Sync(float current, float max, float min = 0)
        {
            this.Set(current, max, min);
            return this;
        }

        private void Set(float current, float max, float min = 0)
        {
            float _min = Mathf.Min(min, max);
            float _max = Mathf.Max(min, max);
            this.min = _min;
            this.max = _max;
            this.diffRange = _max - _min;
            this.SetCurrent(current);
        }

        public void SetCurrent(float current)
        {
            this.SetCurrentReturnIsChanged(current);
        }

        public bool SetCurrentReturnIsChanged(float current)
        {
            var curr = Mathf.Clamp(current, this.min, this.max);
            bool isChanged = curr != this.current;
            this.current = curr;
            return isChanged;
        }

        public bool AddCurrentReturnIsChanged(float addNum)
        {
            float current = this.current + addNum;
            return this.SetCurrentReturnIsChanged(current);
        }

        public void Clear()
        {
            this.max = 0;
            this.min = 0;
            this.current = 0;
            this.diffRange = 0;
        }

        #region 对象池
        public uint MaxStore => 1;

        public void OnReset()
        {
            this.Clear();
        }

        public void Put2Pool()
        {
            ClassPool.PutAutoType(this);
        }

        static public NumerCappedRange NewFromPool()
        {
            return ClassPool<NumerCappedRange>.Get();
        }
        #endregion

    }
}
